import os
import sys
from PySide6.QtWidgets import QWidget, QGridLayout, QLabel, QPushButton, QMessageBox, QLineEdit, QComboBox, QFileDialog, QApplication
from easy_tools.utils.file import load_file_data, write_data


class TextFileConversionWidget(QWidget):

    def __init__(self):
        super().__init__()
        self._init()

    def _init(self):
        grid = QGridLayout()
        self.setLayout(grid)
        self.input_path, self.output_path = '', ''
        self.input_show_widget = QLabel('', self)
        self.input_widget = QPushButton('选择文件', self)
        self.input_file_encoding = QLineEdit('utf-8')
        self.output_show_widget = QLabel('', self)
        self.output_widget = QPushButton('选择目录', self)
        self.output_file_encoding = QLineEdit('utf-8', self)
        self.file_type_widget = QComboBox(self)
        self.file_type_widget.addItems(['csv', 'json', 'yaml', 'yml', 'properties'])
        self.confirm = QPushButton('转换', self)
        self.header = QComboBox(self)
        self.header.addItem('否', False)
        self.header.addItem('是', True)

        x = 0
        grid.addWidget(QLabel('原始文件: ', self), x, 0, 1, 2)
        grid.addWidget(self.input_show_widget, x, 2, 1, 2)
        grid.addWidget(self.input_widget, x, 4, 1, 2)
        grid.addWidget(QLabel('文件编码: ', self), x, 6)
        grid.addWidget(self.input_file_encoding, x, 7)
        x += 1
        grid.addWidget(QLabel('导出目录: ', self), x, 0, 1, 2)
        grid.addWidget(self.output_show_widget, x, 2, 1, 2)
        grid.addWidget(self.output_widget, x, 4, 1, 2)
        grid.addWidget(QLabel('文件编码: ', self), x, 6)
        grid.addWidget(self.output_file_encoding, x, 7)
        x += 1
        grid.addWidget(QLabel('导出文件类型: ', self), x, 0)
        grid.addWidget(self.file_type_widget, x, 1)
        grid.addWidget(QLabel('是否含表头: ', self), x, 2)
        grid.addWidget(self.header, x, 3)
        x += 1
        grid.addWidget(self.confirm, x, 0, 1, 8)
        for i in range(x + 1):
            grid.setRowStretch(i, 1)

        self.input_widget.clicked.connect(self._choice_input_file)
        self.output_widget.clicked.connect(self._choice_output_dir)
        self.confirm.clicked.connect(self._convert)

    def _choice_input_file(self):
        file_name, _ = QFileDialog.getOpenFileName(self, filter='All Files (*.csv *.json *.yaml *.yml *.properties)')
        if file_name:
            self.input_show_widget.setText(file_name)
            self.input_widget.setText('重新选择')
            # 默认生成
            self.output_show_widget.setText(file_name[:-1 - file_name[::-1].index('/')])
            self.output_widget.setText('重新选择')

    def _choice_output_dir(self):
        dir_name = QFileDialog.getExistingDirectory(self)
        if dir_name:
            self.output_show_widget.setText(dir_name)
            self.output_widget.setText('重新选择')

    def _convert(self):
        if not self.input_show_widget.text():
            QMessageBox.question(self, '错误', '请选择一个原始文件', QMessageBox.StandardButton.Ok)
            return
        if not self.output_show_widget.text():
            QMessageBox.question(self, '错误', '请选择一个导出目录', QMessageBox.StandardButton.Ok)
        input_path = self.input_show_widget.text()
        output_path = self.output_show_widget.text() + os.sep + input_path.split('/')[-1].split('.')[0] + '.' + self.file_type_widget.currentText()
        data = load_file_data(input_path, self.input_file_encoding.text())
        write_data(output_path, data, self.header.currentData(), self.output_file_encoding.text())
        QMessageBox.question(self, "提示", "转换完成", QMessageBox.StandardButton.Ok)


if __name__ == '__main__':
    app = QApplication()
    win = TextFileConversionWidget()
    win.show()
    sys.exit(app.exec())
